// pages/createaccount/createaccount.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"",
    init_amount:"0.00",
    color:"#5779E7",
    type_data:['资产','负债'],
    type_index:0,
    type:"资产",
    icon:"",
    id:0,
    sub_type_data:['现金','银行卡','信用卡','应收','应付账'],
    sub_type_index:0,
    sub_type:"现金",
    currency:"EUR",
    currencys:[{
      tag:"CNY",
      name:"人民币"
    },
    {
      tag:"EUR",
      name:"欧元"
    }],
    key_name:"tag",
    currencyIndex:0
  },
  bindMultiPickerChange(e) {
    var that = this;
    this.setData({
      currencyIndex:e.detail.value,
      currency: that.data.currencys[e.detail.value].tag
    })
  },
  onClose(e){

  },
  onDel(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定删除此账户吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          that.doRemove();
        } 
      }
    })
  },
  doRemove(){


    var that = this;
    var data = that.data;
    request.requestAjax('account/removeAccount',{id:that.data.id},"正在移除",function(res){
        if(res.status==200){
          wx.showToast({
            title: '移除成功',
            icon:"success"
          })
          setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 3];
        
            if(Reflect.has(prevPage,"getAccounts")){
              prevPage.getAccounts();
            }
              wx.navigateBack({
                delta:2
              });
          },1000);
            return false;
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none"
          })
        }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
    });
  },
  selectSubTypeScope(events){

    this.setData({
      sub_type_index:events.detail.value,
      sub_type:this.data.sub_type_data[events.detail.value]
    })
    this.onClose()
  },
  selectTypeScope(events){

    this.setData({
      type_index:events.detail.value,
      type:this.data.type_data[events.detail.value]
    })
    this.onClose()
  },
  onSubmit(){
      var that = this;
      var data = that.data;
      request.requestAjax('account/saveAccount',data,"加载中",function(res){
        if(res.status==200){
          wx.showToast({
            title: '保存成功',
            icon:"success"
          })
          setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
        
            if(Reflect.has(prevPage,"getAccounts")){
              prevPage.getAccounts();
            }
              wx.navigateBack();
          },1000);
            return false;
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none"
          })
        }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
    });
  },
  getCurrency(){
    var that = this;
    request.requestAjax('company/getCurrency',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              currencys:res.data
            })
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  getInfo(){
    var that = this;
    request.requestAjax('company/accountInfo',{id:this.data.id},"加载中",function(res){
        if(res.status==200){
            that.setData({
              title:res.data.title,
              init_amount:res.data.init_amount,
              color:res.data.color,
              currency:res.data.currency,
              type_index:res.data.data.type_index,
              type:res.data.data.type,
              icon:res.data.icon,
              id:res.data.id,
              sub_type_index:res.data.data.sub_type_index,
              sub_type:res.data.data.sub_type
            });
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCurrency();
    if(options.id!=undefined){
      this.setData({id:options.id})
      this.getInfo();
    }
  },
  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})